//
//  SettingViewController.m
//  jungoo
//
//  Created by Dang Quang Trung on 7/25/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "SettingViewController.h"
#import "PrivacyViewController.h"
#import "FeedbackViewController.h"
#import "BlockedViewController.h"
#import "AccountViewController.h"

@implementation SettingViewController

@synthesize settingCell;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

-(void)applyAppearance 
{ 
    [self setBackButtonImageActiveName:nil inactiveName:@"back-button" target:self action:@selector(backPressed)];
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"home_bg.png"]]]; 
}

-(void) backPressed 
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidLoad
{
    
    [super viewDidLoad];
       
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.opaque = NO;
    self.tableView.backgroundView = nil;    
    
    [JGoUtility showProgressInView:self.view 
                            target:self 
                             title:@"Loading Settings..." 
                          selector:@selector(delayLoadBlockedPeople:)];
}

-(void)viewWillAppear:(BOOL)animated
{
    [self applyAppearance];    
    [self setTitle:@"Settings"];
    [self.tableView reloadData];
    [super viewWillAppear:animated];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)dealloc
{
    [self setTableView:nil];
    [settingCell release];
    [super dealloc];
}

#pragma mark - Table Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{    
        if (section == 1) {
            return 3;
        }  else {      
            return 1;
        }        
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
            
        static NSString *CellIdentifier = @"SettingCell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell  = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
            [[NSBundle mainBundle] loadNibNamed:@"SettingCell" owner:self options:nil];
            cell = settingCell;
            self.settingCell = nil;
        }
        UIImageView *icon = (UIImageView *)[cell viewWithTag:1];
        
        UILabel *firstText = (UILabel *)[cell viewWithTag:2];
        UILabel *secondText = (UILabel *)[cell viewWithTag:3];
        switch (indexPath.section) {
            case 0:
            {
                firstText.text = @"Privacy";
                icon.image = [UIImage imageNamed:@"btn_eye.png"];
                break;
            }
            case 1:
            {
                switch (indexPath.row) {
                    case 0:
                    {
                        firstText.text = @"About";
                        icon.image = [UIImage imageNamed:@"about-icon.png"];
                        break;
                    }
                    case 1:
                    {
                        firstText.text = @"Account";
                        icon.image = [UIImage imageNamed:@"email-icon.png"];
                        break;
                    }
                    case 2:
                    {
                        firstText.text = @"Feedback";
                        icon.image = [UIImage imageNamed:@"questions-icon.png"];
                        break;
                    }
                        
                    default:
                        break;
                }
                break;
                
            }
           
            case 2:
            {
                icon.image = [UIImage imageNamed:@"pass-icon.png"];
                firstText.text = @"Blocked";
                secondText.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
                secondText.text = [NSString stringWithFormat:@"%d",[dataSource count]];
                [secondText sizeToFit];
                break;
            }
            default:
                break;
        }
        firstText.textColor = [UIColor colorWithRed:0. green:0.678 blue:0.941 alpha:1];
        [firstText sizeToFit];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
           switch (indexPath.section) {
            case 0:
            {
                PrivacyViewController *pVC = [[PrivacyViewController alloc] initWithNibName:@"PrivacyViewController" 
                                                                                   bundle:nil];
                [self.navigationController pushViewController:pVC animated:YES];
                [pVC release];
               
                break;
            }
            case 1:
            {
                switch (indexPath.row) {
                    case 0:
                    {
                        /*
                        PickerViewController *pVC = [[PickerViewController alloc] initWithNibName:@"PickerViewController" 
                                                                                           bundle:nil];
                        pVC.mode = @"courses";
                        pVC.title = @"Course";
                        pVC.selectedItem = selectedCourse;
                        pVC.arrayNo = listCourse;
                        [self.navigationController pushViewController:pVC animated:YES];
                        [pVC release];
                         */
                        break;
                         
                    }
                    case 1:
                    {
                        
                        AccountViewController *aVC = [[AccountViewController alloc] initWithNibName:@"AccountViewController" 
                                                                                               bundle:nil];
                        [self.navigationController pushViewController:aVC animated:YES];
                        [aVC release];
                        break;
                    }
                    case 2:
                    {
                        
                        FeedbackViewController *fVC = [[FeedbackViewController alloc] initWithNibName:@"FeedbackViewController" 
                                                                                           bundle:nil];
                        [self.navigationController pushViewController:fVC animated:YES];
                        [fVC release];
                        break;
                    }
                    default:
                        break;
                }
                break;
                
            }
            case 2:
            {
                BlockedViewController *bVC = [[BlockedViewController alloc] initWithNibName:@"BlockedViewController" 
                                                                                       bundle:nil];
                [bVC setBlockedPeople:(id)dataSource];
                [self.navigationController pushViewController:bVC animated:YES];
                [bVC release];

                break;
                   
            }
            
            default:
                break;
        }
    
}

#pragma mark - Delay Method

- (void)delayLoadBlockedPeople:(MBProgressHUD*)progress
{
    NSError *error = nil;
    NSString *userId = [[JGoUtility sharedCreateAccount] objectForKey:@"user_id"];
    NSArray *blockedPeople = [JGoAPI getPeopleBlockedWithUserId:userId error:&error];
    
    if (!error) {
        dataSource = (id)[blockedPeople retain];
        [self.tableView reloadData];
    }
    else {
        [JGoUtility showAlert:[error localizedDescription]];
    }
    
    [progress hide:YES];
}

@end
